import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { Component, Input, OnInit } from '@angular/core';
import { ModalController, NavParams } from '@ionic/angular';

@Component({
  selector: 'app-modal-multiple-select3-full-screen',
  templateUrl: './modal-multiple-select3-full-screen.component.html',
  styleUrls: ['./modal-multiple-select3-full-screen.component.scss'],
})
export class ModalMultipleSelect3FullScreenComponent implements OnInit {
  @Input() data: any;
  showCheckAll: any; // 三级选 是否包含全选
  secondMax: any; // 2级选
  thirdMax: any; // 3级选
  constructor(
    public modalController: ModalController,
    private appGlobalService: AppGlobalService,
    public navParams: NavParams,
  ) { }
  // 三级为品牌的
  ngOnInit() {
    this.data  =  JSON.parse(JSON.stringify(this.navParams.data.value));
    this.showCheckAll = this.data.checkAll;
    this.secondMax = this.data.secondMax ? this.data.secondMax : 0;
    this.thirdMax = this.data.thirdMax ? this.data.thirdMax : 0;
    if ( this.data && this.data.children && this.data.children.length > 0) {
      this.data.children[0].open = true;
      this.data.current = this.data.children[0];
    }
    this.data.children.forEach(element => {
      if (element.checked) {
        this.data.children[0].open = false;
        element.open = true;
        this.data.current = element;
      }
    });
    // 初始值
    let lastValue: any;
    if (this.data.value && typeof(this.data.value).isArray  ) {
      lastValue = this.data.value;

    } else if (this.data.value && typeof(this.data.value).isString) {
      lastValue = this.data.value ? this.data.value.split(',') : [];
    } else {
      lastValue = [];
    }
    this.data.value = lastValue;
    if (lastValue.length > 0) {
      this.data.children.forEach(element => {

        if (element.children && element.children.length > 0 ) {
         element.children.forEach(e => {
            if (lastValue.indexOf (e.value) > -1 ) {
              e.checked = true;
              element.hasValue = true;
            }
            if (e.children && e.children.length > 0) {

              e.children.forEach(i => {
                if (lastValue.indexOf (i.value) > -1 ) {
                  i.checked = true;
                  element.hasValue = true;
                }
              });
           }
         });
       }
     });
    }

  }

  chooseCurrentAll(item) {
    item.current.checked = !item.current.checked;
    if (item.current.checked) {
      item.current.hasValue = true;
      item.current.children.forEach(element => {
        element.checked = true;
        // element.children.forEach(e => {
        //   e.checked = true;

        // });
      });
    } else {
      item.current.hasValue = false;
      item.current.children.forEach(element => {
        element.checked = false;
        element.children.forEach(e => {
          e.checked = false;

        });
      });
    }
    this.checkBigVaue(item);
    // console.log(item.value);
  }
  checkBigVaue(item) {
      // 遍历 二 三级
      let n = 0;
      item.current.children.forEach(element => {
        if (element.checked) {
          ++n;
          item.current.hasValue = true;
          if (item.value.findIndex(v => (v === element.value)) === -1) {
            // +2级
            item.value.push(element.value);
          }
          element.children.forEach(e => {
            if (e.checked) {
              item.current.hasValue = true;
              if (item.value.findIndex(v => (v === e.value)) === -1) {
              // +3级
                item.value.push(e.value);
              }
            } else {
              // 移除3级
              // 移除2级
              const index = item.value.findIndex(v => (v === e.value));
              if (index > -1) {
                item.value.splice(index, 1);
              }

            }
          });

        } else {
          // 移除2级
          const index = item.value.findIndex(v => (v === element.value));
          if (index > -1) {
            item.value.splice(index, 1);
          }
          // 移除3级
          this.removeValue(item, element);
        }

      });
      if (n === item.current.children.length ) {
        item.current.checked = true;
      } else {
        item.current.checked = false;
      }
      console.log(item.value);
  }

  removeValue(item, it) {
    it.children.forEach(e => {
      const index = item.value.findIndex(v => (v === e.value));
      if (index > -1) {
        item.value.splice(index, 1);

      }
    });
  }
  // 左边大类点击
  itemLeftClick(it) {
    this.data.children.forEach(element => {
      element.open = false;
    });
    it.open = true;
    this.data.current = it;
  }
  tagClick(tag, grade?, secondTag?) {
    tag.checked = !tag.checked;
    if (this.secondMax && this.secondMax > 0 && grade === 2) {
      let a = 0;
      if ( this.secondMax === 1) {
        this.data.current.children.forEach(element => {
          if (element !== tag) {
            element.checked = false;
              // 三级品牌取消
            if (element.children && element.children.length > 0) {
              element.children.forEach(e => {
                e.checked = false;
              });
            }
          }
        });
      } else {
        this.data.current.children.forEach(element => {
          if (element.checked) {
            ++a;
          }
        });
      }

      if (a > this.secondMax) {
        tag.checked = !tag.checked;
        this.appGlobalService.showToast("最多只能选择" + this.secondMax + "项", 1500, "middle");
        return;
      }

    } else if (this.thirdMax && this.thirdMax > 0 && grade === 3) {
      let a = 0;
      secondTag.children.forEach(element => {
        if (element.checked) {
          ++a;
        }
      });
      if (a > this.thirdMax) {
        tag.checked = !tag.checked;
        this.appGlobalService.showToast("最多只能选择" + this.thirdMax + "项", 1500, "middle");
        return;
      }
    }

    if (tag.checked) {
      this.data.current.hasValue = true;
    } else {
        let hasValue = false;
        this.data.current.children.forEach(element => {
          if (element.checked) {
            hasValue = true;
          } else {
            // 三级品牌取消
            if (element.children && element.children.length > 0) {
              element.children.forEach(e => {
                e.checked = false;
              });
            }
          }

        });

        this.data.current.hasValue = hasValue;
    }
    this.checkBigVaue(this.data);
  }

  resetClick() {
    this.data.children.forEach(element => {
       element.checked = false;
       element.hasValue = false;
       if (element.children && element.children.length > 0) {
        element.children.forEach(e => {
          e.checked = false;
          if (e.children && e.children.length > 0) {
            e.children.forEach(i => {
              i.checked = false;

            });
          }
        });
      }
    });
  }
  sureClick() {
    // const labelArray: any = [];
    const valueArray: any = [];
    this.data.children.forEach(element => {

      if (element.children && element.children.length > 0 && element.hasValue) {
       element.children.forEach(e => {
          if (e.checked) {
            valueArray.push(e);
          }
          if (e.children && e.children.length > 0) {

          e.children.forEach(i => {
             if (i.checked) {
              valueArray.push(i);

             }

           });
         }
       });
     }
   });

    this.modalController.dismiss(valueArray);

  }
  onCancelClick(e) {
    e.stopPropagation();
  }
  dismissClick() {
    this.modalController.dismiss();
  }

}